package com.example.yiwei.crm.view.activity.coordinationActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.managerDao;
import com.example.yiwei.crm.dao_impl.managerDaoImpl;
import com.example.yiwei.crm.data.GetAreData;
import com.example.yiwei.crm.data.GetDock;
import com.example.yiwei.crm.entity.dockingEntity;
import com.example.yiwei.crm.entity.managerEntity;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.util.ZhengZhe;
import com.example.yiwei.crm.view.activity.areActivity.ProvinceActivity;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;
import com.example.yiwei.crm.view.frament.JDCoorFragment.EnteringFragment;
import com.example.yiwei.crm.view.frament.JDCoorFragment.QueryFragment;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;



public class AddCoorActivity extends AppCompatActivity {
    private Spinner spinner_manager;
    private EditText edit_docking_name, edit_docking_mechanism, edit_mechanism_attribute, edit_department,
            edit_professional_title, edit_phone_number, edit_additional, edit_xadress, edit_jd;
    private TextView txt_address,txt_title;
    private Button btn_show;
    private ImageView img_back, img_commit;
    private JSONObject mJsonObj;
    private String Filename;
    private List<String> name_list = new ArrayList<>();
    List<managerEntity> managerList;
    private ArrayAdapter adapter1;
    private String managerName;
    private int manager_id;
    String manager_name;
    SharedPreferences sp;
    dockingEntity docking;
    String tx;
    String pid;
    String pname = "";
    String cid;
    String cname = "";
    String aid;
    String aname = "";
    Button commit;
    private ImageView img_search,img_luru;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_coor);
        txt_title = (TextView) findViewById(R.id.healt_education_add_txt_title);
        if ("update".equals(getIntent().getStringExtra("tag"))) {
            txt_title.setText("修改合作伙伴");
        }

        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        img_search= (ImageView) findViewById(R.id.item_image_cx);
        img_luru= (ImageView) findViewById(R.id.item_image_lr);
        img_back = (ImageView) findViewById(R.id.health_education_add_img_back);
        img_commit = (ImageView) findViewById(R.id.health_education_add_img_commit);
        btn_show = (Button) findViewById(R.id.health_education_add_btn_show);
        txt_address = (TextView) findViewById(R.id.health_education_add_txt_address);
        edit_docking_name = (EditText) findViewById(R.id.health_education_add_edit_docking_name);
        edit_docking_mechanism = (EditText) findViewById(R.id.health_education_add_edit_docking_mechanism);
        edit_mechanism_attribute = (EditText) findViewById(R.id.health_education_add_edit_mechanism_attribute);
        edit_department = (EditText) findViewById(R.id.health_education_add_edit_department);
        edit_professional_title = (EditText) findViewById(R.id.health_education_add_edit_professional_title);
        edit_phone_number = (EditText) findViewById(R.id.health_education_add_edit_phone_number);
        edit_additional = (EditText) findViewById(R.id.health_education_add_edit_additional);
        edit_xadress = (EditText) findViewById(R.id.health_education_add_edit_xadress);
        edit_jd = (EditText) findViewById(R.id.health_education_add_edit_jd);
        spinner_manager = (Spinner) findViewById(R.id.health_education_add_spinner_manager);
        commit = (Button) findViewById(R.id.add_coor_btn);
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (txt_address.getText().toString().equals("")||edit_docking_name.getText().toString().equals("")) {
                    Toast.makeText(AddCoorActivity.this, "请填写行政区域和姓名！", Toast.LENGTH_SHORT).show();
                }else if (!"".equals(edit_phone_number.getText().toString())&& !ZhengZhe.phone(edit_phone_number.getText().toString())){
                    Toast.makeText(AddCoorActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                }else {
                    add();
                }
            }
        });
        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddCoorActivity.this, ProvinceActivity.class);
                GetAreData.pname = null;
                GetAreData.aname = null;
                GetAreData.cname = null;
                GetAreData.pid = null;
                GetAreData.cid = null;
                GetAreData.aid = null;
                startActivity(intent);
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                try {
                    object.put("admin", sp.getString("admin", null));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                managerDao manaDao = new managerDaoImpl();
                managerList = manaDao.queryAllManager(baseClass.query_manager_url, json);
                for (int i = 0; i < managerList.size(); i++) {
                    Log.d("aaa", managerList.get(i).toString());
                }
                for (int i2 = 0; i2 < managerList.size(); i2++) {
                    name_list.add(managerList.get(i2).getManager_name());
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter1 = new ArrayAdapter(AddCoorActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, name_list);
                        adapter1.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        spinner_manager.setAdapter(adapter1);
                        spinner_manager.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                managerName = spinner_manager.getSelectedItem().toString();
                                for (int i1 = 0; i1 < managerList.size(); i1++) {
                                    if (managerName.equals(managerList.get(i1).getManager_name())) {
                                        manager_id = Integer.valueOf(managerList.get(i1).getManager_id());
                                    }
                                }
                                if ("update".equals(getIntent().getStringExtra("tag"))) {
                                    showMessage();
                                }
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> adapterView) {


                            }
                        });
                    }
                });
            }
        }).start();
      img_search.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent intent=new Intent(AddCoorActivity.this,JDcoorActivity.class);
              intent.putExtra("ii","ii");
              startActivity(intent);
              finish();
          }
      });
        img_luru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(AddCoorActivity.this,JDcoorActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
    public void add(){


        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);

                try {
                    if ("update".equals(getIntent().getStringExtra("tag"))) {
                        docking = GetDock.get();
                        object.put("KXA01", docking.getDocking_id());
                        object.put("addoredit", "edit");
                    } else {
                        object.put("addoredit", "add");
                    }
                    object.put("KXA15", managerName);
                    object.put("KXA28", manager_id);
                    object.put("KXA02", edit_docking_name.getText().toString());
                    object.put("KXA03", edit_docking_mechanism.getText().toString());
                    object.put("KXA04", edit_mechanism_attribute.getText().toString());
                    object.put("KXA05", edit_department.getText().toString());
                    object.put("KXA06", edit_professional_title.getText().toString());
                    object.put("KXA08", edit_phone_number.getText().toString());
                    object.put("KXA29", txt_address.getText().toString());
                    object.put("REK01", edit_additional.getText().toString());
//                                object.put("KXA17", pid);
                    object.put("CID03", sp.getString("CID03", null));
                    if (!pname.equals("")){
                        object.put("KXA17", pid);
                        object.put("KXA18",pname);
                    }
                    if (!cname.equals("")){
                        object.put("KXA19", cid);
                        object.put("KXA20",cname);
                    }
                    if (!aname.equals("")){
                        object.put("KXA21", aid);
                        object.put("KXA22",aname);
                    }
                    object.put("KXA27", edit_jd.getText().toString());
                    object.put("KXA29", edit_xadress.getText().toString());
                    String json = object.toString();
                    String result;
                    result = NetWorkUtils.getCount(baseClass.edit_coordianation_URL, json);
                    JSONObject object1 = new JSONObject(result);
                    String boolen = object1.getString("ifsuccess");
                    if ("true".equals(boolen)) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if ("update".equals(getIntent().getStringExtra("tag"))) {
                                    Toast.makeText(AddCoorActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(AddCoorActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                                }
                                finish();
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                img_commit.setVisibility(View.VISIBLE);
                                Toast.makeText(AddCoorActivity.this, "保存失败，请重试", Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    public void showMessage() {
        docking = GetDock.get();
        edit_department.setText(docking.getDepartment());
        edit_docking_name.setText(docking.getDocking_name());
        edit_docking_mechanism.setText(docking.getDocking_mechanism());
        edit_mechanism_attribute.setText(docking.getMechanism_attribute());
        edit_professional_title.setText(docking.getProfessional_title());
        edit_phone_number.setText(docking.getPhone_number());
//        System.out.print("iuuyyyy"+docking.getPname() + docking.getCname() + docking.getAname()+"llll");
        if (docking.getAname() != null && docking.getPname() != null && docking.getCname() != null) {
            txt_address.setText(docking.getPname() + docking.getCname() + docking.getAname());
        }
        edit_additional.setText(docking.getAddtional_remark());
        edit_xadress.setText(docking.getAddress());
        edit_jd.setText(docking.getJd());
        SpinnerAdapter spadapter2 = spinner_manager.getAdapter();
        for (int i = 0; i < managerList.size(); i++) {
            if (docking.getManager_id() == managerList.get(i).getManager_id()) {
                manager_name = managerList.get(i).getManager_name();
            }
        }
        if (docking.getManager() != null) {
            for (int i = 0; i < spadapter2.getCount(); i++) {
                if (docking.getManager().equals(spadapter2.getItem(i).toString())) {
                    spinner_manager.setSelection(i);
                }
            }
        }
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        if (GetAreData.pname!=null){
            pid = GetAreData.pid;
            pname = GetAreData.pname;
            cid = GetAreData.cid;
            cname = GetAreData.cname;
            aid = GetAreData.aid;
            aname =GetAreData.aname;
            txt_address.setText(pname+cname+aname);
        }
    }
}
